using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using WebService.ServiceContracts;

namespace WebService.ServiceImplementation
{
    [ServiceBehavior(Name = "PokerGame", Namespace = "http://WebService.ServiceContracts/2007/10",
        InstanceContextMode = InstanceContextMode.PerSession,
        ConcurrencyMode = ConcurrencyMode.Single)]
    public class PokerGame : WebService.ServiceContracts.IPokerGame
    {
        private WebService.ServiceImplementation.PokerGameService.PokerGameClient client;
        #region IPokerGame Members

        public joinResponse Join(JoinRequest request)
        {
            joinResponse response = new joinResponse(false);
            try
            {
                client = new WebService.ServiceImplementation.PokerGameService.PokerGameClient();
                if (this.client != null && this.client.State != CommunicationState.Opened)
                {
                    this.client.Open();
                }
                WebService.ServiceImplementation.PokerGameService.PokerPlayer player = new WebService.ServiceImplementation.PokerGameService.PokerPlayer();
                player.ID = request.player.ID;
                response.check = client.Join(player, request.GameID);
                client.Close();
            }
            finally
            {
                if (this.client != null && client.State != CommunicationState.Closed)
                {
                    client.Close();
                }
            }
            return response;
        }

        public betResponse Bet(betRequest request)
        {
            betResponse response = new betResponse();
            try
            {
                client = new WebService.ServiceImplementation.PokerGameService.PokerGameClient();
                if (this.client != null && this.client.State != CommunicationState.Opened)
                {
                    this.client.Open();
                }
                response.check = client.Bet(request.bet, request.ID, request.ID);
                client.Close();
            }
            finally
            {
                if (this.client != null && this.client.State != CommunicationState.Closed)
                {
                    client.Close();
                }
            }
            return response;

        }

        public tableResponse GetTable(tableRequest request)
        {
            tableResponse response = new tableResponse();
            response.table = new WebService.DataContracts.PokerTable();
            try
            {
                client = new WebService.ServiceImplementation.PokerGameService.PokerGameClient();
                if (this.client != null && this.client.State != CommunicationState.Opened)
                {
                    this.client.Open();
                }
                WebService.ServiceImplementation.PokerGameService.PokerTable table = client.GetTable(request.PlayerID, request.GameID);
                client.Close();
                response.table.BetPool = table.BetPool;
                List<WebService.DataContracts.PokerPlayer> l = new List<WebService.DataContracts.PokerPlayer>();
                foreach (WebService.ServiceImplementation.PokerGameService.PokerPlayer p in table.Players)
                {
                    WebService.DataContracts.PokerPlayer pla = new WebService.DataContracts.PokerPlayer();
                    pla.ActualBet = p.ActualBet;
                    pla.Card1 = new WebService.DataContracts.Card();
                    pla.Card1.Color = (WebService.DataContracts.CardColor)Enum.Parse(typeof(WebService.DataContracts.CardColor), p.Card1.Color.ToString());
                    pla.Card1.Face = (WebService.DataContracts.CardFace)Enum.Parse(typeof(WebService.DataContracts.CardFace), p.Card1.Face.ToString());
                    pla.Card2 = new WebService.DataContracts.Card();
                    pla.Card2.Color = (WebService.DataContracts.CardColor)Enum.Parse(typeof(WebService.DataContracts.CardColor), p.Card1.Color.ToString());
                    pla.Card2.Face = (WebService.DataContracts.CardFace)Enum.Parse(typeof(WebService.DataContracts.CardFace), p.Card1.Face.ToString());
                    pla.Chair_number = p.Chair_number;
                    pla.ID = p.ID;
                    pla.Money = p.Money;
                    pla.Status = (WebService.DataContracts.ActionList)Enum.Parse(typeof(WebService.DataContracts.ActionList), p.Status.ToString());
                    l.Add(pla);
                }
                response.table.Players = l;
                List<WebService.DataContracts.Card> li = new List<WebService.DataContracts.Card>();
                foreach(WebService.ServiceImplementation.PokerGameService.Card c in table.CardsOnTable)
                {
                    WebService.DataContracts.Card ca = new WebService.DataContracts.Card();
                    ca.Color = (WebService.DataContracts.CardColor)Enum.Parse(typeof(WebService.DataContracts.CardColor), c.Color.ToString());
                    ca.Face =  (WebService.DataContracts.CardFace)Enum.Parse(typeof(WebService.DataContracts.CardFace), c.Face.ToString());
                    li.Add(ca);
                }
                response.table.CardsOnTable = li;
            }
            finally
            {
                if (this.client != null && this.client.State != CommunicationState.Closed)
                {
                    this.client.Close();
                }
            }
            return response;
        }

        public beginResponse Begin(beginRequest request)
        {
            beginResponse response = new beginResponse();
            try
            {
                client = new WebService.ServiceImplementation.PokerGameService.PokerGameClient();
                if (this.client != null && this.client.State != CommunicationState.Opened)
                {
                    this.client.Open();
                }
                response.result = this.client.Begin(request.GameID, request.PlayerID);
                client.Close();
            }
            finally
            {
                if (this.client != null && this.client.State != CommunicationState.Closed)
                {
                    this.client.Close();
                }
            }
            return response;
        }

        public createRoomResponse CreateRoom(createRoomRequest request)
        {
            createRoomResponse response = new createRoomResponse(0);
            try
            {
                client = new WebService.ServiceImplementation.PokerGameService.PokerGameClient();
                if (this.client != null && this.client.State != CommunicationState.Opened)
                {
                    this.client.Open();
                }
                response.Game_id = this.client.CreateRoom(request.RoomName, TranslateBetweenPokerRoomOptionsAndPokerRoomOptions.
                    TranslatePokerRoomOptionsToPokerRoomOptions(request.Settings), request.Owner_id);
                client.Close();
            }
            finally
            {
                if (this.client != null && this.client.State != CommunicationState.Closed)
                {
                    this.client.Close();
                }
            }
            return response;
        }

        public leaveRoomResponse LeaveRoom(leaveRoomRequest request)
        {
            leaveRoomResponse response = new leaveRoomResponse(false);
            try
            {
                client = new WebService.ServiceImplementation.PokerGameService.PokerGameClient();
                if (this.client != null && this.client.State != CommunicationState.Opened)
                {
                    this.client.Open();
                }
                response.check = this.client.LeaveRoom(request.PlayerID, request.GameID);
                client.Close();
            }
            finally
            {
                if (this.client != null && this.client.State != CommunicationState.Closed)
                {
                    this.client.Close();
                }
            }
            return response;
        }

        #endregion

        #region IPokerGame Members


        public AskForChangesResponse AskForChanges(AskForChangesRequest request)
        {
            
            AskForChangesResponse response = new AskForChangesResponse();
           
            return response;
        }

        #endregion
    }
}
